/**
 * Project: shuyin-sync
 * 
 * File Created at Mar 15, 2012
 * $Id$
 * 
 * Copyright 2008 6677bank.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * 6677bank Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with 6677bank.com.
 */
package com.bank6677.sync.csm;

import java.sql.SQLException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bank6677.common.exception.BusinessException;
import com.bank6677.sync.csm.service.ApplicationSyncService;
import com.bank6677.sync.csm.service.UserSyncService;

/**
 * 同步执行器
 * 
 * @author liyue
 * @version SyncExecutor.java Mar 15, 2012 3:08:39 PM
 */
public class SyncExecutor {

    private static ApplicationContext ctx;

    /**
     * <p>
     * Synchronous users information from old version.
     * 
     * @author liyue
     */
    static void syncUsers() {
        UserSyncService userSyncService = ctx.getBean("userSyncServiceImpl", UserSyncService.class);
        try {
            userSyncService.syncCsmUsers();
        } catch (SQLException e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }

    /**
     * <p>
     * Synchronous causes of dropping application.
     * 
     * @author liyue
     */
    static void syncRejectType() {
        ApplicationSyncService applicationSyncService = ctx.getBean("applicationSyncServiceImpl", ApplicationSyncService.class);
        applicationSyncService.syncRejectType();
    }

    /**
     * <p>
     * Synchronous applications.
     * 
     * @author liyue
     */
    static void syncAppilcation() {
        ApplicationSyncService applicationSyncService = ctx.getBean("applicationSyncServiceImpl", ApplicationSyncService.class);
        try {
            long start = System.currentTimeMillis();
//            applicationSyncService.syncApplications();
            applicationSyncService.asyncApps();
//            applicationSyncService.syncApplications("20120301", "20120302");
            System.out.println(System.currentTimeMillis() - start);
        } catch (SQLException e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }
    
    static void syncPost(){
        
    }

    public static void main(String[] args) {
        if (ctx == null) {
            ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        }
        
//        syncUsers();
        //        syncRejectType();
        syncAppilcation();
    }
    
    public static ApplicationContext getCtx(){
        return ctx;
    }
}
